package com.thoughtworks.oo.quantity;

public abstract class Unit {
    private final int conversionFactor;
    private final Unit baseUnit;

    public Unit(int conversionFactor, Unit baseUnit) {
        this.conversionFactor = conversionFactor;
        this.baseUnit = baseUnit;
    }

    public int getBaseAmount(int amount) {
        return getConversionFactor() * amount;
    }

    public Unit getBaseUnit() {
        if (baseUnit == null) {
            return this;
        }
        return baseUnit.getBaseUnit();
    }

    private int getConversionFactor() {
        if (baseUnit == null) {
            return conversionFactor;
        }
        return conversionFactor * baseUnit.getConversionFactor();
    }
}
